/**
 * Created by zzy on 2017/1/13.
 */
var $ = require('jquery');
var $alert = require('libs/alert');
var $confirm = require('libs/confirm')
require('easyui');
var isSale = require('libs/user-type').isSale;
$(function(){
    mainTableInit();
    //查询按钮
    $("#a_search").on("click",function () {
        $("#client_table").datagrid("load",{
            clientId:$("#tb input[name=clientId]").val()
        });
    });
    //删除
    $("body").on("click","a[name=delete]",function () {
           var id = $(this).attr("rowId");
        $confirm("确认删除此资源吗？",function () {
            deleteRow(id);
        })
    })
});

function mainTableInit(){
    $("#client_table").datagrid({
        url:'api/v1/p/mms/client/list',
        border : false,
        height:$(window).height(),
        pagination : true,
        idField : 'id',
        pageSize : 20,
        pageList : [ 20, 50,  100 ],
        checkOnSelect : true,//单击选中
        selectOnCheck : true,
        singleSelect : true,
        emptyMsg : "暂无数据",
        rownumbers:true,
        columns:[[
            {
                field : 'id',
                hidden:true
            },{
                field:'clientId',
                title:'终端代码',
                width:150,
                formatter:function (value,row,index) {
                    return "<a href='client-detail?id="+row.id+"' name='authorityName'>"+value+"</a>"
                }
            },{
                field:'resourceIds',
                title:'资源id',
                width:150,
                formatter:function (value,row,index) {
                    return value;
                }
            },
            {
                field:'clientSecret',
                title:'终端密钥',
                width:150
            },{
                field:'scope',
                title:'Scope',
                width:150
            },{
                field:'authorizedGrantTypes',
                title:'授权类型',
                width:200,
                formatter:function (value,row,index) {
                    return value;
                }
            },{
                field:'webServerRedirectUri',
                title:'跳转地址',
                width:200
            },{
                field:'opertation',
                title:'操作',
                hidden:isSale,
                width:300,
                formatter:function (value,row,index) {
                    return "<a href='client-update?id="+row.id+"' name='modify'>修改</a>|<a href='javascript:;' rowId='"+row.id+"' name='delete'>删除</a>" ;
                }
            }
        ]],
        loadFilter:function(data){
            if(data.code!='200'){
                return data.data;
            }
            return data.data;
        },
        onLoadSuccess:function(data){

        },
        toolbar:"#tb"
    })
    $("#a_create").show();
}

function deleteRow(id) {
    $.ajax({
        url:'api/v1/p/mms/client/delete',
        type:'post',
        data:JSON.stringify({"id":id}),
        contentType: "application/json; charset=utf-8",
        dataType:'json',
        success:function (result) {
            if(result.code == 200){
                $("#client_table").datagrid("reload");
            }else{
                $alert(result.message);
            }
        }
    })
}